﻿using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Drawing;
using SLogo.Helper;

namespace SLogo.Drawing
{
    /// <summary>
    /// 绘图类，实现跟位图有关的绘图动作
    /// </summary>
    public class PngBitmap:SlogoDrawing
    {
        private Bitmap _bitmap;//位图
        private Pen _pen;//画笔
        private Font drawFont = new Font("Arial", 16); //字体
        private double _width;//宽度
        private double _height;//高度

        /// <summary>
        /// 创建画板，颜色默认为白色
        /// </summary>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        public PngBitmap(double width, double height)
        {
            _width = width;
            _height = height;
            _bitmap = new Bitmap((int)width, (int)height);//画布大小
            Graphics g = Graphics.FromImage(_bitmap);
            _pen = new Pen(Color.Black,1);//画笔画线颜色和大小,也是字体的颜色
            g.Clear(Color.White);
            g.Dispose();
        }

        /// <summary>
        /// 设置画笔颜色
        /// </summary>
        /// <param name="color">画笔颜色</param>
        public override void SetPenColor(Color color)
        {
            _pen = new Pen(color, 1); //画笔设置
        }

        /// <summary>
        /// 画直线
        /// </summary>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <param name="x2"></param>
        /// <param name="y2"></param>
        public override void Line(double x1, double y1, double x2, double y2)
        {
           x1 =_width / 2 + x1 ;
           x2 =_width / 2 + x2;
           y1 = _height / 2-y1;
           y2 = _height / 2-y2;
           Graphics g = Graphics.FromImage(_bitmap);
           g.DrawLine(_pen, (float)x1, (float)y1, (float)x2, (float)y2);
           g.Dispose();
        }

        /// <summary>
        /// 画矩形外框
        /// </summary>
        /// <param name="x">中心x坐标</param>
        /// <param name="y">中心y坐标</param>
        /// <param name="width">矩形宽度</param>
        /// <param name="height">矩形高度</param>
        /// <param name="angle">角度</param>
        public override void Rectangle(double x, double y, double width, double height,double angle)
        {
            x = _width / 2 + x ;
            y = _height / 2 - y;
            angle = angle * 180 / Math.PI;
            Graphics g= Graphics.FromImage(_bitmap);
            Matrix ma = new Matrix();//2d图形变换
            ma.RotateAt((float)angle,new PointF((float)x,(float)y), MatrixOrder.Append);//设置角度
            g.Transform = ma;
            x = x - width / 2;
            y = y - height / 2;
            g.DrawRectangle(_pen, (float)x, (float)y, (float)width, (float)height);
            g.Dispose();
        }

        /// <summary>
        /// 填充矩形区域，颜色为color
        /// </summary>
        /// <param name="x">中心x坐标</param>
        /// <param name="y">中心y坐标</param>
        /// <param name="width">矩形宽度</param>
        /// <param name="height">矩形高度</param>
        /// <param name="angle">角度</param>
        /// <param name="color">颜色</param>
        public override void Rectangle(double x, double y, double width, double height,double angle,Color color)
        {

            x = _width / 2 +x;
            y = _height / 2 -y;
            angle = angle * 180 / Math.PI;
            Graphics g = Graphics.FromImage(_bitmap);
            Matrix ma = new Matrix();//2d图形变换
            ma.RotateAt((float)angle, new PointF((float)x, (float)y), MatrixOrder.Append);//设置角度
            g.Transform = ma;
            SolidBrush newBrush = new SolidBrush(color);
            x = x - width / 2;
            y = y - height / 2;
            g.DrawRectangle(_pen, (float)x, (float)y, (float)width, (float)height);
            g.FillRectangle(newBrush, (float)x, (float)y, (float)width, (float)height);
            g.Dispose();
        }

        /// <summary>
        /// 画椭圆框
        /// </summary>
        /// <param name="x">中心x坐标</param>
        /// <param name="y">中心y坐标</param>
        /// <param name="width">矩形范围宽度</param>
        /// <param name="height">矩形范围高度</param>
        /// <param name="angle">角度</param>
        public override void Ellipse(double x, double y, double width, double height,double angle)
        {
            x = _width / 2 + x;
            y = _height / 2 - y;
            angle = angle * 180 / Math.PI;
            Graphics g = Graphics.FromImage(_bitmap);
            Matrix ma = new Matrix();//2d图形变换
            ma.RotateAt((float)angle, new PointF((float)x, (float)y), MatrixOrder.Append);//设置角度
            g.Transform = ma;
            x = x - width / 2;
            y = y - height / 2;
            g.DrawEllipse(_pen, (float)x, (float)y, (float)width, (float)height);
            g.Dispose();
        }

        /// <summary>
        /// 填充椭圆形区域,颜色为color
        /// </summary>
        /// <param name="x">中心x坐标</param>
        /// <param name="y">中心y坐标</param>
        /// <param name="width">矩形范围宽度</param>
        /// <param name="height">矩形范围高度</param>
        /// <param name="angle">角度</param>
        /// <param name="color">颜色</param>
        public override void Ellipse(double x, double y, double width, double height, double angle, Color color)
        {
            x = _width / 2 + x;
            y = _height / 2 - y;
            angle = angle*180/Math.PI;
            Graphics g = Graphics.FromImage(_bitmap);
            Matrix ma = new Matrix();//2d图形变换
            ma.RotateAt((float)angle, new PointF((float)x, (float)y), MatrixOrder.Append);//设置角度
            g.Transform = ma;
            SolidBrush newBrush = new SolidBrush(color);
            x = x - width / 2;
            y = y - height / 2;
            g.FillEllipse(newBrush, (float)x, (float)y, (float)width, (float)height);
            g.DrawEllipse(_pen, (float)x, (float)y, (float)width, (float)height);
            g.Dispose();
        }

        /// <summary>
        /// 画多边形的框
        /// </summary>
        /// <param name="points">多边形依次的坐标点</param>
        public override void Polygon(PointF[] points)
        {
            for(int i=0;i<points.Length;i++)
            {
                points[i].X = (float)_width / 2 + points[i].X;
                points[i].Y = (float)_height / 2 - points[i].Y;
            }
            Graphics g = Graphics.FromImage(_bitmap);
            g.DrawPolygon(_pen, points);
            g.Dispose();
        }


        /// <summary>
        /// 填充多边形框
        /// </summary>
        /// <param name="points">多边形依次的坐标点</param>
        /// <param name="color">填充色</param>
        public override void Polygon(PointF[] points, Color color)
        {
            for (int i = 0; i < points.Length; i++)
            {
                points[i].X = (float)_width / 2 + points[i].X;
                points[i].Y = (float)_height / 2 - points[i].Y;
            }
            Graphics g = Graphics.FromImage(_bitmap);
            SolidBrush newBrush = new SolidBrush(color);
            g.DrawPolygon(_pen, points);
            g.FillPolygon(newBrush, points);
            g.Dispose();
        }


        /// <summary>
        /// 按一定角度，一定坐标输出打印文本
        /// </summary>
        /// <param name="outStr">打印字符串</param>
        /// <param name="angle">角度</param>
        /// <param name="x">坐标x</param>
        /// <param name="y">坐标y</param>
        public override void Text(string outStr, double angle, double x, double y)
        {
            x = _width / 2 + x;
            y = _height / 2-y;
            Graphics g = Graphics.FromImage(_bitmap);
            Matrix ma = new Matrix();//2d图形变换
            PointF point = new PointF((float) x, (float) y);
            ma.RotateAt((float)(angle * 180 / Math.PI), point, MatrixOrder.Prepend);//设置角度
            g.Transform = ma;
            g.DrawString(outStr, drawFont, SystemBrushes.ControlText, (float)x, (float)y);
            g.Dispose();
        }

        /// <summary>
        /// 清除画板内容，颜色设为初始背景色
        /// </summary>
        public override void Clear(Color color)
        {
            Graphics g = Graphics.FromImage(_bitmap);
            g.Clear(color);
            g.Dispose();
        }

       /// <summary>
        ///  裁剪加边框并输出二进制码
       /// </summary>
       /// <param name="x">开始x坐标</param>
       /// <param name="y">开始y坐标</param>
       /// <param name="width">宽度</param>
       /// <param name="height">高度</param>
        ///<param name="top">边框厚度</param>
        public override void Print(double width,double height,double top,double buttom,double left,double right)
       {
           if (top==-1)
           {
               Guid guid = Guid.NewGuid(); //生成Guid随机数
               //保存文件到临时文件夹
               string path = Environment.GetEnvironmentVariable("TEMP") + "\\" + guid + ".temp";
               //保存
               _bitmap.Save(path);
               //打开文件
               FileStream fs = new FileStream(path, FileMode.Open);
               byte[] by = new byte[fs.Length];
               //文件流写入数组
               fs.Read(by, 0, (int)fs.Length);
               //输出数组
               ConsoleHelper.StdOut(by);
               fs.Close();
               //删除临时文件
               File.Delete(path);
           }
           else
           {
               //裁剪图片
               Bitmap map = new Bitmap((int) (width - left - right), (int) (height - top - buttom)); //创建真正拆取的位图大小
               Graphics g = Graphics.FromImage(map); //创建map画板
               g.DrawImage(_bitmap, new Rectangle(0, 0, (int) (width - left - right), (int) (height - top - buttom)),
                           new Rectangle((int) (_width/2 - width/2), (int) (_height/2 - width/2),
                                         (int) (width - left - right), (int) (height - top - buttom)),
                           GraphicsUnit.Pixel);
               g.Dispose(); //释放画板资源
               //加边框
               Bitmap bmpOut = new Bitmap((int) width, (int) height); //最后得到的图片
               Graphics gOut = Graphics.FromImage(bmpOut); //创建bmpout画板
               Rectangle rec = new Rectangle(0, 0, map.Width, map.Height);
               //gOut.DrawLine(new Pen(Command.TurtleCommand.BackgroundColor, (int) left), 0, 0, 0, (int) height);
               //gOut.DrawLine(new Pen(SLogo.Command.TurtleCommand.BackgroundColor, (int) right), (int) width, 0,
               //              (int) width, (int) height);
               //gOut.DrawLine(new Pen(SLogo.Command.TurtleCommand.BackgroundColor, (int) top), 0, 0, (int) width, 0);
               //gOut.DrawLine(new Pen(SLogo.Command.TurtleCommand.BackgroundColor, (int) buttom), 0, (int) height,
               //              (int) width, (int) height);
               gOut.Dispose();
               Guid guid = Guid.NewGuid(); //生成Guid随机数
               //保存文件到临时文件夹
               string path = Environment.GetEnvironmentVariable("TEMP") + "\\" + guid + ".temp";
               //保存
               bmpOut.Save(path);
               //打开文件
               FileStream fs = new FileStream(path, FileMode.Open);
               byte[] by = new byte[fs.Length];
               //文件流写入数组
               fs.Read(by, 0, (int) fs.Length);
               //输出数组
               ConsoleHelper.StdOut(by);
               fs.Close();
               //删除临时文件
               File.Delete(path);
           }
       }
    }
}
